#!/system/bin/sh
# Project WIPE https://github.com/yc9559/cpufreq-interactive-opt
# Author: yc9559
# Generated at: Sat Jun  2 19:21:28 2018

# $1:value $2:file path
function set_value() {
	if [ -f $2 ]; then
		# chown 0.0 $2
		chmod 0666 $2
		echo $1 > $2
		chmod 0444 $2
	fi
}

# $1:display-name $2:file path
function print_value() {
	if [ -f $2 ]; then
		echo $1
		cat $2
	fi
}

# $1:cpu0 $2:timer_rate $3:value
function set_param() {
	echo $3 > /sys/devices/system/cpu/$1/cpufreq/interactive/$2
}

# $1:cpu0 $2:timer_rate
function print_param() {
	echo "$1: $2"
	cat /sys/devices/system/cpu/$1/cpufreq/interactive/$2
}

# $1:io-scheduler $2:block-path
function set_io() {
	if [ -f $2/queue/scheduler ]; then
		if [ `grep -c $1 $2/queue/scheduler` = 1 ]; then
			echo $1 > $2/queue/scheduler
			echo 512 > $2/queue/read_ahead_kb
			set_value 0 $2/queue/iostats
			set_value 256 $2/queue/nr_requests
			set_value 0 $2/queue/iosched/slice_idle
		fi
	fi
}

action=$1
info="Generated at: Sat Jun  2 19:21:28 2018"
if [ ! -n "$action" ]; then
    action="balance"
fi

if [ "$action" = "debug" ]; then
	echo $info
	print_value "cpu0:min_freq" /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
	print_param cpu0 above_hispeed_delay
	print_param cpu0 target_loads
	print_value "sched_spill_load" /proc/sys/kernel/sched_spill_load
	print_value "eMMC IO scheduler" /sys/block/mmcblk0/queue/scheduler
	print_value "UFS IO scheduler" /sys/block/sda/queue/scheduler
	which perfd
	exit 0
fi

# SELinux permissive
setenforce 0
# disable hotplug to switch governor
echo 1 > /sys/module/msm_thermal/core_control/enabled
echo N > /sys/module/msm_thermal/parameters/enabled

echo 1 > /sys/devices/system/cpu/cpu4/online
chown 0.0 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chown 0.0 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*
set_value 380000 /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
set_value 380000 /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# set flag
	touch /dev/project_wipe_runonce
	# Perfd, nothing to worry about, if error the script will continue
	stop perfd

	set_value 90 /proc/sys/kernel/sched_spill_load
	set_value 1 /proc/sys/kernel/sched_prefer_sync_wakee_to_waker
	set_value 40 /proc/sys/kernel/sched_init_task_load
	set_value 3000000 /proc/sys/kernel/sched_freq_inc_notify

	# avoid permission problem, do not set 0444
	echo 2-3 > /dev/cpuset/background/cpus
	echo 2-3 > /dev/cpuset/system-background/cpus
	echo 0-3,4-5 > /dev/cpuset/foreground/cpus
	echo 0-3,4-5 > /dev/cpuset/top-app/cpus

	set_value 20 /sys/module/cpu_boost/parameters/input_boost_ms
	set_value 0 /sys/module/msm_performance/parameters/touchboost

	set_value 2 /sys/devices/system/cpu/cpu4/core_ctl/min_cpus
	set_value 2 /sys/devices/system/cpu/cpu4/core_ctl/max_cpus

	set_value "0:1344000 4:1440000" /sys/module/msm_performance/parameters/cpu_max_freq
	set_value 1344000 /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
	set_value 1440000 /sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq

	set_io cfq /sys/block/mmcblk0

	# shared interactive parameters
	set_param cpu0 timer_rate 20000
	set_param cpu4 timer_rate 20000
	set_param cpu0 timer_slack 180000
	set_param cpu4 timer_slack 180000
	set_param cpu0 boost 0
	set_param cpu4 boost 0
	set_param cpu0 boostpulse_duration 0
	set_param cpu4 boostpulse_duration 0
	set_param cpu0 use_sched_load 1
	set_param cpu4 use_sched_load 1
	set_param cpu0 ignore_hispeed_on_notif 0
	set_param cpu4 ignore_hispeed_on_notif 0
fi

if [ "$action" = "powersave" ]; then
	set_value "0:1180000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 above_hispeed_delay "98000 1280000:18000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 97
	set_param cpu0 target_loads "80 580000:46 680000:16 780000:63 880000:91 1180000:98"
	set_param cpu0 min_sample_time 18000
	set_param cpu4 above_hispeed_delay "18000 1180000:98000 1380000:78000"
	set_param cpu4 hispeed_freq 880000
	set_param cpu4 go_hispeed_load 97
	set_param cpu4 target_loads "80 580000:44 680000:58 780000:71 880000:96"
	set_param cpu4 min_sample_time 38000
fi

if [ "$action" = "balance" ]; then
	set_value "0:1180000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 above_hispeed_delay "98000 1280000:58000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 96
	set_param cpu0 target_loads "80 580000:60 680000:51 780000:69 880000:91 1180000:98"
	set_param cpu0 min_sample_time 38000
	set_param cpu4 above_hispeed_delay "18000 1180000:98000 1380000:38000"
	set_param cpu4 hispeed_freq 880000
	set_param cpu4 go_hispeed_load 98
	set_param cpu4 target_loads "80 480000:42 580000:63 680000:50 780000:19 880000:96"
	set_param cpu4 min_sample_time 58000
fi

if [ "$action" = "performance" ]; then
	set_value "0:1180000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 above_hispeed_delay "38000 1280000:98000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 98
	set_param cpu0 target_loads "80 580000:61 680000:54 780000:62 880000:70 1180000:98"
	set_param cpu0 min_sample_time 38000
	set_param cpu4 above_hispeed_delay "18000 1180000:58000 1280000:38000"
	set_param cpu4 hispeed_freq 880000
	set_param cpu4 go_hispeed_load 98
	set_param cpu4 target_loads "80 480000:42 580000:66 680000:61 780000:11 880000:89 1180000:77 1280000:98"
	set_param cpu4 min_sample_time 78000
fi

if [ "$action" = "fast" ]; then
	set_value 980000 /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
	set_value 1080000 /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq
	set_param cpu0 above_hispeed_delay "18000"
	set_param cpu4 above_hispeed_delay "18000"
	set_param cpu0 target_loads "80"
	set_param cpu4 target_loads "80 1480000:97"
	set_param cpu0 min_sample_time 38000
	set_param cpu4 min_sample_time 38000
fi

chmod 0444 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chmod 0444 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*

exit 0